/**
 * 判断属性是否存在并且为true
 * @param { string } t
 */
export const isTrue = (t) => {
  return t || (typeof t === "undefined" && !t);
};

/**
 * 对象数组深拷贝
 * @returns Object
 */
export const deepClone = (obj, hash = new WeakMap()) => {
  if (obj instanceof Date) return new Date(obj);
  if (obj instanceof RegExp) return new RegExp(obj);
  if (typeof obj !== "object") return obj;
  if (hash.get(obj)) return hash.get(obj);
  let cloneObj = new obj.constructor();
  hash.set(obj, cloneObj);
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      cloneObj[key] = deepClone(obj[key], hash);
    }
  }
  return cloneObj;
};

/**
 * 根据唯一标识对数组的对象进行去重
 * @param { array } arr 数组
 * @param { string } key 唯一标识
 * @returns
 */
export const duplicateArr = (arr, key) => {
  var obj = {};
  return arr.reduce(function (item, next) {
    obj[next[key]] ? "" : (obj[next[key]] = true && item.push(next));
    return item;
  }, []);
};

/**
 * 表单数据回选
 * @param { array } autoFrom
 * @param { object } values
 * @returns
 */
export const fillFormValue = (autoFrom, values) => {
  const newForm = JSON.parse(JSON.stringify(autoFrom));
  return newForm.map((item) => {
    return {
      ...item,
      value: values[item.prop],
    };
  });
};
